package com.cjhedu.web.controller;

import com.cjhedu.web.domain.Dept;
import com.cjhedu.web.service.DeptService;
import com.cjhedu.web.utils.MyThreadLocalUtils;
import com.cjhedu.web.utils.Result;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 陈佳豪
 * @version 1.0
 */
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping("depts")
    public Result getAll(){
        Map<String, Object> value = MyThreadLocalUtils.getValue();
        System.out.println("从ThreadLocal解析的令牌是===========" + value);
        List<Dept> all = deptService.getAll();
        return Result.success(all);
    }

    @DeleteMapping("depts/{id}")
    public Result del(@PathVariable Integer id){
        int del = deptService.del(id);
        return del > 0 ? Result.success() : Result.error(del+"为空，删除失败");
    }

    @PostMapping("depts")
    public Result add(@RequestBody Dept dept){
        int add = deptService.add(dept);
        return add > 0 ? Result.success() : Result.error("添加失败，请重试");
    }

    @PutMapping("update")
    public Result update(@RequestBody Dept dept){
        int update = deptService.update(dept);
        return update > 0 ? Result.success() : Result.error(dept.getId() + "修改失败");
    }
}
